
<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_lookup
    //
	class t_field_tab extends t_base_field
	{
        // property declaration
		// protected
		// C
		protected $link_components_list = array();
		// P
		protected $pages = array();
		// U
		protected $url_parameters_list = null;

        // property declaration
		// public
		public $body_tag = null;
		const component_property_id = 0;
		const component_property_method = 1;
		const component_property_argument = 2;
		const component_property_count = 3;
		// G
		const language_position_last = 1;
		//N
		const no_selection_body_style = '_no_selection_body';
		const no_selection_style = '_no_selection';
		// P
		const page_property_name = 0;
		const page_property_label = 1;
		const page_property_component_list = 2;
		const page_property_count = 3;
        const proper_properties_position_per_pages = 1;		
        const proper_properties_position_per_reload_page_on_click = 2;
        const proper_properties_position_last = 3;
		// S
		const selection_body_style = 'selection_body';
		const selection_style = '_selection';
        // T
		const tag_postfix_body = "_body";
		const tag_postfix_pages = "_pages";
		const tag_postfix_page = "_page_%s";
		//
		// method declaration
    	// protected

        // C
        protected function check_pages()
        {
            $local_return = count($this->pages) > 0; 
			if ($local_return)
				foreach($this->pages as $local_page)
				{
					$local_return = (count((array)$local_page) == self::page_property_count);
					if (!$local_return)
						return false;
					else
					{
                        $local_link_components_list = $local_page[self::page_property_component_list];
						$this->link_components_list[] = $local_link_components_list;
                        //print_r($this->link_components_list);echo "<br>";
						foreach($local_link_components_list as $local_component)
						{
							$local_return = (count((array)$local_component) == self::component_property_count);
                            if (!$local_return)
                                return false;
						}
					}
				}	
                
			return $local_return;
        } 		
        // G
        static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}		
		static function get_default_proper_property($a_index)
		{
			$local_return = '';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = 'TRUE';
				break;                
			}*/
			return $local_return;
		}
		protected function get_proper_properties_position_last()      
		{
            return self::proper_properties_position_last;
        } 
		protected function get_call_back_actions_names()
		{
			return '[]';
		}		
		protected function get_javascript_events_list_initialization()
		{
			
		}		
		// L
        protected function load_url_parameters_list()
        {
            global $g_url_parameters_namespace;
            global $g_cuteengine;
            $local_childs_list = array();
            foreach($this->link_components_list as $local_component_list)
            {
                foreach($local_component_list as $local_component_property)
                {
                   // print_r($local_component_property);echo"<br>";
                    //echo "load_url_parameters_list($component_id)<br>";
                    $local_page_component_id = $local_component_property[self::component_property_id];
                    if ($g_url_parameters_namespace->get_parameter_by_component_id($local_page_component_id) === null)
					{
						//echo "local_page_component_id:$local_page_component_id<br>";
						$local_childs_list[$local_page_component_id] = $g_cuteengine->components[$local_page_component_id];
					}	
                    
                    /*
                        [t_cuteengine::components_cuteengine_component];
                    if (isset($local_component_list))
                        $local_childs_list = array_merge($local_childs_list, $local_component_list->page_component->get_childs_list());*/
                }
            }
			$this->url_parameters_list->copy_from($g_url_parameters_namespace,$local_childs_list); 
        } 
		
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
            parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
			$this->url_parameters_list = new t_url_parameters_list(false);
			$this->pages = json_decode($this->proper_properties_array[self::proper_properties_position_per_pages]);
			
			if (!$this->check_pages())
				throw new Exception('Error creating object class '.get_class($this).
					" named: \"$this->name\". Wrong required page property number were specified");
			
		}

		// C
		// this method permit to add method or function used to manage data in javascript script
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;

		}
		public function create_tags_structure()
        {
            //echo __METHOD__."<BR>";
            parent::create_tags_structure();
			global $g_javascript_namespace;
			
			$local_current_tag = parent::add_tag(t_base_tag_type::btt_ul,
				$this->get_tag_name(self::tag_postfix_pages,array()));
			$this->main_tag = $local_current_tag;	
			$this->body_tag = parent::add_tag(t_base_tag_type::btt_div,
				$this->get_tag_name(self::tag_postfix_body,array()));
			$this->parent_component->set_to_childs_components_tag($this->body_tag);
			$this->body_tag->class_name = $this->get_tag_name(self::no_selection_body_style,array());
			//STYLE
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array()),'float','left');
    		$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array()),'line-height','1em');
    		$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array()),'list-style','none outside none');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array()),'margin','0');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array()),'padding','0 0 1px 1px');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array()),'position','relative');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array()),'width','100%');

			# LI 
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." LI",'float','left');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." LI",'margin','0');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." LI",'padding','0');


			// A 
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'display','block');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'text-decoration','none');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'font-weight','bold');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'margin','0');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'padding','0.25em 1em');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'border-left','1px solid #fff');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'border-top','1px solid #fff');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A",'border-right','1px solid #aaa');


			# A:hover,
			# A:active,
			# A.here:link,
			# A.here:visited 
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A:hover",'background','#bbb');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A:active",'background','#bbb');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A.here:link",'background','#bbb');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A.here:visited",'background','#bbb');				

			# A.here:link,
			# A.here:visited 
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A.here:link",'position','relative');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_pages,array())." A.here:visited",'z-index','102');

			//SELECTION
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::selection_style,array()),'color','red');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::selection_style,array()),'background-color','darkGrey');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::no_selection_style,array()),'color','#444');  
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::no_selection_style,array()),'background-color','#DDD');  
			// BODY STYLE
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_body,array()),"display","inline-block");
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_body,array()),'position','relative');
			$this->add_style_sheet_property(
				"#".$this->get_tag_name(self::tag_postfix_body,array()),'top','-1px');			
			
			// SELECTION BODY		
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::selection_body_style,array()),'background-color','darkGrey');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::selection_body_style,array()),'border','1px solid black');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::selection_body_style,array()),'padding-top','5px');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::selection_body_style,array()),'padding-left','5px');

			// NO SELECTION BODY		
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::no_selection_body_style,array()),'background-color','#DDD');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::no_selection_body_style,array()),'border','1px solid black');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::no_selection_body_style,array()),'padding-top','5px');
			$this->add_style_sheet_property(
				".".$this->get_tag_name(self::no_selection_body_style,array()),'padding-left','5px');

				
            
        }
        //F
        public function finalize_creation_field()
        {
			parent::finalize_creation_field();
            global $g_ambient_namespace;
            global $g_url_parameters_namespace;
            $local_parent_parameter = $g_url_parameters_namespace->get_parameter_by_component_id($this->parent_component->id);	
			// PREPARE URL LINK
            if ($this->get_reload_page_on_click())
			{
				$this->load_url_parameters_list();
                $local_url = $g_ambient_namespace->get_absolute_web_file_path(false);
				$local_parameters_list = new t_url_parameters_list(false);
                if ($this->url_parameters_list->is_disguised_string())
					$local_common_param_string = $this->url_parameters_list->compute_disguised_parameters_string(true,false,false);
				else	
					$local_common_param_string = $this->url_parameters_list->compute_http_predefined_variables(false,false,false);
				//echo "local_common_param_string: $local_common_param_string<br>";
			}		
            foreach($this->pages as $local_page_index => $local_page)
			{
                
                
                // URL PARAMETERS LIST
                	
                $local_parameters_list->clear_parameters();
                // TAG
				$local_current_tag = $this->main_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_li,
					$this->get_tag_name(self::tag_postfix_page,array($local_page[self::page_property_name])));
				//print_r($this->link_components_list); echo "<br>";
                $local_component_list = $this->link_components_list[$local_page_index];
                $local_selected = true;
                foreach($local_component_list as $local_component_property)
                {
                    //echo "local_component_property:"; print_r($local_component_property); echo "<br>";
                    //$a_query_parameter->function_result .= "<li onclick='". t_cuteengine::javascript_instance_name.".".$this->name."." ."list_item_selected(event,\"".$this->main_tag->name."\",\"$local_key_str\",\"$local_labels_str2\")'>". $local_labels_str ."</li>";
                    
                    if ($this->get_reload_page_on_click())
                    {
                        // URL PARAMETERS LIST
                        //echo "local_parent_parameter->method: $local_parent_parameter->method <br>";
                        if (! $local_parent_parameter->has_default_values())
                            $local_parameters_list->add_new_parameter($local_parent_parameter->component_id,
                                $local_parent_parameter->method,$local_parent_parameter->argument);
                        // 
                        $local_parameters_list->add_new_parameter(-1,
                            t_url_parameter::default_method_label,'');
                        $local_parameter = $local_parameters_list->get_parameter_by_component_id(-1);	
                        if ($local_component_property[self::component_property_method] != '')
                            $local_parameter->method = $local_component_property[self::component_property_method];
                        else	
                            $local_parameter->method = t_url_parameter::default_method_label;
                        $local_parameter->argument = implode(t_url_parameter::separator_level_1,$local_component_property[self::component_property_argument]);
                        
                        $local_parameter->component_id = $local_component_property[self::component_property_id];
						// SELECTED
						$local_global_parameter = $this->url_parameters_list->get_parameter_by_component_id($local_component_property[self::component_property_id]);
                        $local_global_method = $local_global_parameter->method;
                        if ($local_global_parameter->method == '')
                            $local_global_method = t_url_parameter::default_method_label;
                        //
                        //if ($local_parameter->argument == '')
                          //  $local_parameter->argument = $local_global_parameter->argument;
                        if ($local_parent_parameter->argument != '')
                        {
                            if ($local_parameter->argument == '')
                                $local_parameter->argument = $local_parent_parameter->argument;
                            else    
                                $local_parameter->argument = $local_parent_parameter->argument.t_url_parameter::separator_level_1.$local_parameter->argument;
                        }
                        //echo "($local_global_parameter->argument == $local_parameter->argument)&& ($local_global_method == $local_parameter->method) <br>";
                        $local_selected = (($local_selected) && ($local_global_parameter != null) && (
							$local_global_parameter->argument == $local_parameter->argument)&& ($local_global_method == $local_parameter->method));	
                        //
						//echo "local_selected:  $local_selected<br>";
                        
						
                    }
                }    
				if ($this->get_reload_page_on_click())
				{
					//print_r($local_parameters_list->parameters_list); echo "<br>";
					if ($this->url_parameters_list->is_disguised_string())
						$local_param_string = $local_parameters_list->compute_disguised_parameters_string(false,true,true);
					else
						$local_param_string = $local_parameters_list->compute_http_predefined_variables(true,true,true);
						
                    //echo "local_param_string: $local_param_string<br>";
					//
                    $local_current_tag = $local_current_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_a,
                        $this->get_tag_name(self::tag_postfix_page,array($local_page[self::page_property_name])). "_link");
                    $local_current_tag->add_text_to_include($local_page[self::page_property_label]);	
                    $local_current_tag->add_property('href',$local_url.$local_common_param_string.$local_param_string);
                    if ($local_selected)
					{
                        $this->body_tag->class_name = $this->get_tag_name(self::selection_body_style,array());
						$local_current_tag->class_name = $this->get_tag_name(self::selection_style,array());
					}	
                    else	
					{
						$local_current_tag->class_name = $this->get_tag_name(self::no_selection_style,array());
					}	
                }
                else
                {
                    $local_current_tag->add_text_to_include($local_page[self::page_property_label]);	
                    //$local_current_tag->add_property('onclick',t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
                }	
				
			}
        }
        // G
        public function get_event_name($a_event_type)
        {

            return $this->name . "_event_".$a_event_type;
        }
		// gets result script for action based on event type
        public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
	
			switch ($a_event_type) 
            {
                /***********  t_base_field::field_event_type_on_edit ***********/
                case t_base_field::field_event_type_on_edit:
				break;
                /***********  t_base_field::field_event_type_on_cancel ***********/
                case t_base_field::field_event_type_on_cancel:
					
				break;
            }
        }
        
        public function get_result_tag()
        {	
            parent::get_result_tag();
            return $this->main_tag;
		} 
        public function get_start_tag()
        {	
            parent::get_start_tag();
            return $this->main_tag;
		} 		
		
        public function get_tags_structure($a_query_parameter,$a_current_row_no,$a_rows_count,
            $a_result)
        {	
            
            
            
			
            //    $a_result->add_function_result(
              //      "","","","set_tag_style_property(['display'],[''],'".$this->get_tag_name(self::tag_prefix_tent,array()) ."');",true);  
                    
                   
        } 	

        // I
/*		public function initialize_db_fields($a_query_parameter,$a_field,$a_field_no,$a_fields_count,
                            $a_row_no,$local_rows_count)
        {
            if ($a_field->primary_key == 1)
                // key_db_fields
                $this->key_db_fields[] = $a_field->name;
            else
            {
                echo "a_field->name:".$a_field->name."<br>";
                // value_db_fields
                $this->value_db_fields[] = $a_field->name;
            }    
		}		*/
		public function initialize_field_event($a_event_type)
        {			
			
            
		}
        
		//sets html code to fire event type action on html element
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";
			
			switch ($a_event_type) 
            {
                case t_base_field::field_event_type_on_edit:
					$this->main_tag->add_property('onkeydown',t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
				break;                
			}
		}
		
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		
		public function get_reload_page_on_click()
        {			
			return (strtoupper($this->proper_properties_array[self::proper_properties_position_per_reload_page_on_click]) === 'TRUE');
				
		}

		
        
    }
?>    